﻿using System;
using System.Collections.Generic;
using System.Text;
using Zhuang.Utility.Common.Encryption;
using Zhuang.Utility.Common.ConfigFile;


namespace Zhuang.WinForm.AppCode.Common
{
    class SystemUtility
    {
        static public string serverName = "";
        static public string databaseName = "";
        static public string databasePort = "";
        static public string dbUserName = "";
        static public string dbUserPassword = "";

        static public string userName = "";
        static public string userPassword = "";


        public static string connectionString = "";

        public static  ZhuangDB.ZhuangDbService zhuangDbService = new ZhuangDB.ZhuangDbService();

        static SystemUtility()
        {
            ReloadSettings();

            LoadWebService();
            
        }

        private static void LoadWebService()
        {
            //ZhuangDB.ZhuangDbService zhuangDbService = new ZhuangDB.ZhuangDbService();
            zhuangDbService.Url = System.Configuration.ConfigurationManager.AppSettings["WebServiceUrl"].Trim();
            ZhuangDB.MySoapHeader soapHeader = new ZhuangDB.MySoapHeader();
            soapHeader.UserName = "zwb";
            soapHeader.UserPassword = "123";
            zhuangDbService.MySoapHeaderValue = soapHeader;
        }

        public static void ReloadSettings()
        {
            Crypto ch = new Crypto();

            serverName = System.Configuration.ConfigurationManager.AppSettings["ServerName"].Trim();
            databaseName = System.Configuration.ConfigurationManager.AppSettings["DatabaseName"].Trim();
            databasePort = System.Configuration.ConfigurationManager.AppSettings["DatabasePort"].Trim();
            dbUserName = System.Configuration.ConfigurationManager.AppSettings["DbUserName"].Trim();
            dbUserPassword = ch.GetDecryptedValue(System.Configuration.ConfigurationManager.AppSettings["DbUserPassword"].Trim());

            connectionString = @"Data Source="+serverName+","+databasePort+";Initial Catalog="+databaseName+";Persist Security Info=True;User ID="+dbUserName+"; PassWord="+dbUserPassword;


            userName = System.Configuration.ConfigurationManager.AppSettings["UserName"].Trim();
            userPassword = System.Configuration.ConfigurationManager.AppSettings["UserPassword"].Trim();
        }

        public static void SaveAppSetting(string key,string value)
        {

            if (key == "UserPassword")
            { 

                Crypto ch=new Crypto();

                value = ch.GetEncryptedValue(value);

            }

            AppConfigHelper.SaveAppSetting(key,value);

        }
    }
}
